using System.Collections;
using UnityEngine;

public class DisconnectHandler : MonoBehaviour
{
	public float waitingTime = 15f;

	public MultiplayerState state;

	protected float timeWaitingForForOpponent;

	protected bool exitedCurrently;

	protected float realTimeWhenReturned;

	protected float realTimeWhenFirstExited;

	private DisconnectHandlerDelegate myDelegate;

	private string supportRoomNameGUID;

	public int maxExits
	{
		get
		{
			return ConfigBase.instance.maxDisconnects;
		}
	}

	protected GGNetwork network
	{
		get
		{
			return GGNetwork.instance;
		}
	}

	public int exits { get; protected set; }

	public int opponentExits { get; protected set; }

	public bool hasExitedInThisChain { get; protected set; }

	public bool hasOpponentExitedInThisChain { get; protected set; }

	public bool isConnectionOk
	{
		get
		{
			return state == MultiplayerState.BothConnected;
		}
	}

	public void Init(DisconnectHandlerDelegate myDelegate, string supportRoomNameGUID)
	{
		this.myDelegate = myDelegate;
		this.supportRoomNameGUID = supportRoomNameGUID;
		hasExitedInThisChain = false;
		hasOpponentExitedInThisChain = false;
		Debug.Log(this.supportRoomNameGUID);
	}

	private void OnJoinedLobby()
	{
		Debug.Log("Joined Lobby " + state);
		Debug.Log(supportRoomNameGUID);
		StartCoroutine(DoCreateRoom());
	}

	private IEnumerator DoCreateRoom()
	{
		while (!network.CreateRoom(supportRoomNameGUID, null, 2, false))
		{
			float time = 0f;
			float duration = 0.5f;
			while (time < duration)
			{
				time += RealTime.deltaTime;
				yield return null;
			}
		}
	}

	private void OnCreatedRoom()
	{
		Debug.Log("Created Room");
		exitedCurrently = false;
		state = MultiplayerState.RoomCreator;
		if (!hasOpponentExitedInThisChain)
		{
			hasOpponentExitedInThisChain = true;
		}
	}

	private void OnPhotonCreateRoomFailed()
	{
		Debug.Log("Create Room Failed ");
		Debug.Log(supportRoomNameGUID);
		state = MultiplayerState.RoomJoin;
		network.JoinRoom(supportRoomNameGUID);
	}

	private void OnPhotonJoinRoomFailed()
	{
		Debug.Log("Join Room Failed ");
		OnJoinedLobby();
	}

	public void StopServer()
	{
		Debug.Log("STOP SERVER");
		if (state == MultiplayerState.BothConnected)
		{
			realTimeWhenFirstExited = Time.realtimeSinceStartup;
		}
		network.StopServer();
		state = MultiplayerState.NeetToConnect;
		myDelegate.OnDisconnect();
		exitedCurrently = true;
		if (!hasExitedInThisChain)
		{
			exits++;
		}
		hasExitedInThisChain = true;
	}

	public void ReconnectToServer()
	{
		realTimeWhenReturned = Time.realtimeSinceStartup;
		float num = Time.realtimeSinceStartup - realTimeWhenFirstExited;
		if (num > waitingTime || exits > maxExits)
		{
			myDelegate.StopShowingWaitingTime();
			string msg = ((exits <= maxExits) ? "Disconnected! You forfeit the match!" : "Too many disconnects! You forfeited the match.");
			myDelegate.OnGameShouldFinishBecauseOfDisconnect(true, msg);
			network.StopServer();
		}
		else
		{
			network.Start(ConfigBase.instance.GetMatchServerAppName());
		}
	}

	private IEnumerator DoDelayedStopStart()
	{
		network.StopServer();
		float time = 0f;
		while (time < 1f)
		{
			time += RealTime.deltaTime;
			yield return null;
		}
		network.Start(ConfigBase.instance.GetMatchServerAppName());
	}

	private void Update()
	{
		if (!myDelegate.isGameRunning())
		{
			return;
		}
		if (state == MultiplayerState.BothConnected)
		{
			if (network.ConnectedPlayers() >= 1)
			{
				return;
			}
			myDelegate.OnDisconnect();
			if (!GGSupportMenu.instance.isNetworkConnected())
			{
				return;
			}
			realTimeWhenFirstExited = Time.realtimeSinceStartup;
			opponentExits++;
			hasOpponentExitedInThisChain = true;
			Debug.Log("opponent disconnects: " + opponentExits);
			if (opponentExits > maxExits)
			{
				myDelegate.StopShowingWaitingTime();
				myDelegate.OnGameShouldFinishBecauseOfDisconnect(false, "You win! Opponent forfeited!");
				return;
			}
			state = MultiplayerState.WaitingForOpponent;
			if (Application.isEditor)
			{
				StartCoroutine(DoDelayedStopStart());
				return;
			}
			network.StopServer();
			network.Start(ConfigBase.instance.GetMatchServerAppName());
		}
		else if (network.ConnectedPlayers() > 0)
		{
			myDelegate.StopShowingWaitingTime();
			bool currentPlayerWasWaiting = !hasExitedInThisChain;
			myDelegate.OnReconnect(currentPlayerWasWaiting);
			exitedCurrently = false;
			state = MultiplayerState.BothConnected;
			hasExitedInThisChain = false;
			hasOpponentExitedInThisChain = false;
		}
		else if (!exitedCurrently)
		{
			float num = Time.realtimeSinceStartup - realTimeWhenFirstExited;
			myDelegate.UpdateWaitingTime(Mathf.Max(0f, waitingTime + 1.5f - num));
			if (num > waitingTime + 1.5f)
			{
				myDelegate.StopShowingWaitingTime();
				myDelegate.OnGameShouldFinishBecauseOfDisconnect(false, "Opponent timed out!");
			}
		}
	}
}
